import {getJSON, postJSON} from '../utils/request';
import api from '../constants/api';
import Taro from '@tarojs/taro';

// 请求首页数据
export function getTopicList(params) {
    return async dispatch => {
        let result = await getJSON(api.gettopics, params);
        if (result && result.data) {
            console.log(result.data);
            if (result.data.success) {
                dispatch({type: 'getTopicList', list: result.data.data})
            }
        }
    }
}

// 请求下页数据
export function getNextList(params) {
    return async dispatch => {
        let result = await getJSON(api.gettopics, params);
        if (result && result.data) {
            console.log(result.data);
            if (result.data.success) {
                if (result.data.data.length > 0) {
                    dispatch({type: 'appendTopicList', list: result.data.data, page:params.page})
                }  
            }
        }
    }
}

// 请求话题详情
export function getTopicInfo(params) {
    return async dispatch => {
        let result = await getJSON(api.gettopicinfo + params.id, params)
        if (result && result.data && result.data.success) {
            dispatch({type:'getTopicInfo', infoData: result.data.data})
        } else {
            console.error('请求话题失败!');
        }
    }
}

// 点赞
export function admireTopic(params) {
    return async dispatch => {
        let result = await postJSON(api.upreply + params.replyid + '/ups', params);
        if (result && result.data && result.data.success) {
            // 成功点赞
            Taro.showToast({title: '点赞失败', icon: 'success'})
            dispatch({type: 'admireSuccess'}); 
        } else {
            // 点赞失败
            Taro.showToast({title: '点赞失败', icon: 'none'})
        }
    }
}